﻿using CommunityToolkit.Mvvm.Messaging;
using Company.Core.Interfaces;
using Company.Core.Logger;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using Company.Shell.ViewModels;
using Microsoft.Extensions.DependencyInjection;

namespace Company.Shell.Views
{
    /// <summary>
    /// ShellView.xaml 的交互逻辑
    /// </summary>
    public partial class ShellView : HandyControl.Controls.Window
    {
        public ShellView(
            ICacheManager cacheManager,
            IHardwareMananger hardwareMananger,
            IConfigProvider configProvider,
            IConfigManager configManager)
        {
            InitializeComponent();
            DataContext = App.Current.Services.GetService<ShellViewModel>();

            //退出系统
            WeakReferenceMessenger.Default.Register<ExitSystemMessage>(this, (sender, e) => 
            {
                Logs.Info(e.Value);
                Close();
            });

            //善后工作
            WeakReferenceMessenger.Default.Register<OnExitMessage>(this, (sender, e) =>
            {
                configManager.Write(Core.Enums.ConfigType.System, configProvider.Config);
                configManager.Write(Core.Enums.ConfigType.Cache, cacheManager.Caches);
                configManager.Write(Core.Enums.ConfigType.Alarm, configProvider.AlarmItems);
                hardwareMananger.Close();
            });
        }
    }
}
